package com.nanjia.boot.delivery.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.delivery.model.form.DeliveryAddressForm;
import com.nanjia.boot.delivery.model.query.DeliveryAddressQuery;

import com.nanjia.boot.delivery.model.vo.DeliveryAddressVO;

import com.nanjia.boot.delivery.service.DeliveryAddressService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收货/收票管理前端控制层
 *
 * @author 周洋
 * @since 2025-07-03 10:49
 */
@Tag(name = "收货/收票管理接口")
@RestController
@RequestMapping("/api/v1/delivery-address")
@RequiredArgsConstructor
public class DeliveryAddressController  {

    private final DeliveryAddressService deliveryAddressService;

    @Operation(summary = "收货/收票管理分页列表")
    @GetMapping("/page")
    // @PreAuthorize("@ss.hasPerm('DeliveryAddress:delivery-address:query')")
    public PageResult<DeliveryAddressVO> getDeliveryAddressPage(DeliveryAddressQuery queryParams ) {
        IPage<DeliveryAddressVO> result = deliveryAddressService.getDeliveryAddressPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增收货/收票管理")
    @PostMapping
    // @PreAuthorize("@ss.hasPerm('DeliveryAddress:delivery-address:add')")
    public Result<Void> saveDeliveryAddress(@RequestBody @Valid DeliveryAddressForm formData , HttpServletRequest request) {
        boolean result = deliveryAddressService.saveDeliveryAddress(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取收货/收票管理表单数据")
    @GetMapping("/{id}/form")
    // @PreAuthorize("@ss.hasPerm('DeliveryAddress:delivery-address:edit')")
    public Result<DeliveryAddressForm> getDeliveryAddressForm(
        @Parameter(description = "收货/收票管理ID") @PathVariable Long id
    ) {
        DeliveryAddressForm formData = deliveryAddressService.getDeliveryAddressFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改收货/收票管理")
    @PutMapping(value = "/{id}")
    // @PreAuthorize("@ss.hasPerm('DeliveryAddress:delivery-address:edit')")
    public Result<Void> updateDeliveryAddress(
            @Parameter(description = "收货/收票管理ID") @PathVariable Long id,
            @RequestBody @Validated DeliveryAddressForm formData
    ) {
        boolean result = deliveryAddressService.updateDeliveryAddress(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除收货/收票管理")
    @DeleteMapping("/{ids}")
    // @PreAuthorize("@ss.hasPerm('DeliveryAddress:delivery-address:delete')")
    public Result<Void> deleteDeliveryAddresss(
        @Parameter(description = "收货/收票管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = deliveryAddressService.deleteDeliveryAddresss(ids);
        return Result.judge(result);
    }


    @Operation(summary = "收货/收票管理分页列表")
    @GetMapping("/list")
    // @PreAuthorize("@ss.hasPerm('DeliveryAddress:delivery-address:query')")
    public Result<List<DeliveryAddressVO>> getDeliveryAddresslist(DeliveryAddressQuery queryParams ) {
        List<DeliveryAddressVO> result = deliveryAddressService.getDeliveryAddresslist(queryParams);
        return Result.success(result);
    }

}
